<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="月台代码" prop="platformCode">
        <el-input
          v-model="queryParams.platformCode"
          placeholder="请输入月台代码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月台名称" prop="platformName">
        <el-input
          v-model="queryParams.platformName"
          placeholder="请输入月台名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入所属仓库"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月台类型" prop="platformType">
        <el-select v-model="queryParams.platformType" placeholder="请选择月台类型" clearable style="width: 240px">
          <el-option :label="platformTypeEnum.RECEIVING.label" :value="platformTypeEnum.RECEIVING.code" />
          <el-option :label="platformTypeEnum.SHIPPING.label" :value="platformTypeEnum.SHIPPING.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="月台状态" prop="platformStatus">
        <el-select v-model="queryParams.platformStatus" placeholder="请选择月台状态" clearable style="width: 240px">
          <el-option :label="platformStatusEnum.IDLE.label" :value="platformStatusEnum.IDLE.code" />
          <el-option :label="platformStatusEnum.OCCUPIED.label" :value="platformStatusEnum.OCCUPIED.code" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:platform:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:platform:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:platform:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:platform:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="platformList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="月台ID" align="center" prop="platformId" v-if="false" />
      <el-table-column label="月台代码" align="center" prop="platformCode" />
      <el-table-column label="月台名称" align="center" prop="platformName" />
      <el-table-column label="所属仓库" align="center" prop="warehouseName" />
      <el-table-column label="月台类型" align="center" prop="platformType">
        <template #default="scope">
          <el-tag :type="scope.row.platformType === platformTypeEnum.RECEIVING.code ? 'success' : 'danger'">
            {{ scope.row.platformType === platformTypeEnum.RECEIVING.code ? platformTypeEnum.RECEIVING.label : platformTypeEnum.SHIPPING.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="月台状态" align="center" prop="platformStatus">
        <template #default="scope">
          <el-tag :type="scope.row.platformStatus === platformStatusEnum.IDLE.code ? 'success' : 'warning'">
            {{ scope.row.platformStatus === platformStatusEnum.IDLE.code ? platformStatusEnum.IDLE.label : platformStatusEnum.OCCUPIED.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:platform:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:platform:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改月台对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="platformRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="请选择仓库" style="width: 360px">
            <el-option
              v-for="item in warehouseOptions"
              :key="item.warehouseId"
              :label="item.warehouseName"
              :value="item.warehouseId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="月台代码" prop="platformCode">
          <el-input v-model="form.platformCode" placeholder="请输入月台代码" />
        </el-form-item>
        <el-form-item label="月台名称" prop="platformName">
          <el-input v-model="form.platformName" placeholder="请输入月台名称" />
        </el-form-item>
        <el-form-item label="月台类型" prop="platformType">
          <el-select v-model="form.platformType" placeholder="请选择月台类型">
            <el-option :label="platformTypeEnum.RECEIVING.label" :value="platformTypeEnum.RECEIVING.code" />
            <el-option :label="platformTypeEnum.SHIPPING.label" :value="platformTypeEnum.SHIPPING.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="月台状态">
          <el-radio-group v-model="form.platformStatus">
            <el-radio :label="platformStatusEnum.IDLE.code">{{ platformStatusEnum.IDLE.label }}</el-radio>
            <el-radio :label="platformStatusEnum.OCCUPIED.code">{{ platformStatusEnum.OCCUPIED.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Platform">
import { listPlatform, getPlatform, delPlatform, addPlatform, updatePlatform } from "@/api/wms/base/platform";
import { listWarehouse } from "@/api/wms/base/warehouse";
import { parseTime } from '@/utils/ruoyi';

// 月台类型枚举
const platformTypeEnum = {
  RECEIVING: {
    code: '1',
    label: '收货月台'
  },
  SHIPPING: {
    code: '2',
    label: '发货月台'
  }
};

// 月台状态枚举
const platformStatusEnum = {
  IDLE: {
    code: '0',
    label: '空闲'
  },
  OCCUPIED: {
    code: '1',
    label: '占用'
  }
};

const { proxy } = getCurrentInstance();

const platformList = ref([]);
const warehouseOptions = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    platformCode: null,
    platformName: null,
    platformType: null,
    platformStatus: null,
    warehouseName: null
  },
  rules: {
    warehouseId: [
      { required: true, message: "所属仓库不能为空", trigger: "change" }
    ],
    platformCode: [
      { required: true, message: "月台代码不能为空", trigger: "blur" }
    ],
    platformName: [
      { required: true, message: "月台名称不能为空", trigger: "blur" }
    ],
    platformType: [
      { required: true, message: "月台类型不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询月台列表 */
function getList() {
  loading.value = true;
  listPlatform(queryParams.value).then(response => {
    platformList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询仓库下拉选项 */
function getWarehouseOptions() {
  listWarehouse().then(response => {
    warehouseOptions.value = response.rows;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    platformId: null,
    warehouseId: null,
    platformCode: null,
    platformName: null,
    platformType: null,
    platformStatus: "0",
    remark: null
  };
  proxy.resetForm("platformRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.platformId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getWarehouseOptions();
  open.value = true;
  title.value = "添加月台";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getWarehouseOptions();
  const platformId = row.platformId || ids.value[0];
  getPlatform(platformId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改月台";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["platformRef"].validate(valid => {
    if (valid) {
      if (form.value.platformId != null) {
        updatePlatform(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPlatform(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const platformIds = row.platformId || ids.value;
  proxy.$modal.confirm('是否确认删除月台编号为"' + platformIds + '"的数据项?').then(function() {
    return delPlatform(platformIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/base/platform/export', {
    ...queryParams.value
  }, `platform_${new Date().getTime()}.xlsx`);
}

getList();
</script>